package com.dxf.hchat.controller;

import com.dxf.hchat.pojo.TbFriend;
import com.dxf.hchat.pojo.TbFriendReq;
import com.dxf.hchat.pojo.vo.FriendReq;
import com.dxf.hchat.pojo.vo.Result;
import com.dxf.hchat.pojo.vo.User;
import com.dxf.hchat.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    /**
     * 发起好友请求
     * @return
     */
    @RequestMapping("/sendRequest")
    public Result sendRequest(@RequestBody TbFriendReq tbFriendReq){
        if (tbFriendReq.getFromUserid()==null || tbFriendReq.getToUserid()==null){
            return new Result(false,"参数不合法");
        }

        try {
            friendService.sendRequest(tbFriendReq.getFromUserid(),tbFriendReq.getToUserid());
            return new Result(true,"请求成功");
        } catch (Exception e) {
            return new Result(false,"请求失败");
        }

    }


    /**
     * 获取好友请求列表
     * @return
     */
    @RequestMapping("/findFriendReqByUserid")
    public List<FriendReq> findFriendReqByUserid(String userid){


      return  friendService.findFriendReqByUserid(userid);
    }

    /**
     * 通过好友申请
     * @param reqid
     * @return
     */
    @RequestMapping("/acceptFriendReq")
    public Result acceptFriendReq(String reqid){

        try {
            friendService.acceptFriendReq(reqid);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }


    /**
     * 拒绝好友申请
     * @param reqid
     * @return
     */
    @RequestMapping("/ignoreFriendReq")
    public Result ignoreFriendReq(String reqid){
        try {
            friendService.ignoreFriendReq(reqid);
            return new Result(true, "拒绝成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "拒绝失败");
        }

    }


    /**
     * 查找好友的通讯录
     * @param userid
     * @return
     */
    @RequestMapping("/findFriendByUserid")
    public List<User>  findFriendByUserid(String userid){
      return friendService.findFriendByUserid(userid);
    }

}
